# -*- coding:utf-8 -*-
'''
@File    :   yaml_test.py
@Time    :   2024/11/17 19:58:50
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

import yaml
from datetime import datetime

timestamp = int(datetime.now().timestamp() * 1000)


def road_yaml(filename):
    with open(filename, encoding='utf-8') as f:
        p_obj = yaml.safe_load(f)  #将读取的文件通过safe_load转化
        print(type(p_obj))  #打印转化后的类型
    return p_obj


def road_yaml_all(filename):
    with open(filename, encoding='utf-8') as f:
        obj = yaml.safe_load_all(f)
        print(type(obj))  #打印转化后的类型
        print(obj)
        for x in obj:
            print(x)


p_data = {
    'cases': [{
        'case_name': f'输入正确有的用户名密码 {timestamp}',
        'data': {
            'path': '/admin/auth/login',
            'methon': 'post',
            'params': {
                'username': 'root',
                'password': '123456'
            }
        },
        'assert': {
            'code': 1,
            'message': '成功'
        }
    }]
}

p_data1 = {
    'cases': [{
        'case_name': f'空密码 {timestamp}',
        'data': {
            'path': '/admin/auth/login',
            'methon': 'post',
            'params': {
                'username': 'root',
                'password': ''
            }
        },
        'assert': {
            'code': 1,
            'message': '成功'
        }
    }]
}


def save_yaml(p_data, y_file_name):
    with open(y_file_name, mode='w', encoding='utf-8') as f:
        yaml.safe_dump(p_data, f, allow_unicode=True, sort_keys=False)


def save_yaml_all(p_data, y_file_name):
    with open(y_file_name, mode='w', encoding='utf-8') as f:
        yaml.safe_dump_all(p_data, f, allow_unicode=True, sort_keys=False)


if __name__ == '__main__':
    print(road_yaml('cases\mockapi\yaml_test.yaml'))
    road_yaml_all('cases\mockapi\yaml_test_all.yaml')
    save_yaml(p_data, 'cases\mockapi\y_file.yaml')
    save_yaml_all([p_data, p_data1], 'cases\mockapi\y_file_all.yaml')

    print(timestamp)
